import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import {ReplaySubject} from 'rxjs';
import { TreeNode } from 'primeng/components/common/api';

@Injectable()
export class NodeService {

    constructor(private http: HttpClient) { }
    private fileId : ReplaySubject<any> = new ReplaySubject<any>( 1 );

    copyFile(id,url){
      this.fileId.next(id);
      return this.http.post<any>(url,this.fileId).toPromise().then(function(res){
        if(res){
          this.http.get(url,function(res){
            console.log(res)
          })
        }
      })
    }

    deleteFile(id,url){
      this.fileId.next(id);
      this.http.post(url,id).toPromise().then(function(res){
        if(res){
          console.log('删除成功')
        }
      })
    }
    

    getFiles() {
    return this.http.get<any>('assets/showcase/data/files.json')
      .toPromise()
      .then(res => <TreeNode[]>res.data);
    }

    getLazyFiles() {
    return this.http.get<any>('assets/showcase/data/files-lazy.json')
      .toPromise()
      .then(res => <TreeNode[]>res.data);
    }

    getFilesystem() {
    return this.http.get<any>('assets/showcase/data/filesystem.json')
      .toPromise()
      .then(res => <TreeNode[]>res.data);
    }

    getLazyFilesystem() {
    return this.http.get<any>('assets/showcase/data/filesystem-lazy.json')
      .toPromise()
      .then(res => <TreeNode[]>res.data);
    }


}